<?php

use App\Http\Controllers\V1\Resources\Auth\LoginController;
use App\Http\Controllers\V1\Resources\Auth\UserController;
use App\Http\Controllers\V1\Resources\Components\UploadController;
use App\Http\Controllers\V1\Resources\Config\ConfigController;
use App\Http\Controllers\V1\Resources\Message\FeedbackController;
use App\Http\Controllers\V1\Resources\Message\MessageController;
use App\Http\Controllers\V1\Resources\Safety\ProductionAccidentController;
use App\Http\Controllers\V1\Resources\Safety\QuestionController;
use App\Http\Controllers\V1\Resources\Safety\RiskSourceController;
use App\Http\Controllers\V1\Resources\Safety\SafetyHazardController;
use App\Http\Controllers\V1\Resources\Checkin\CheckinController;
use App\Http\Controllers\V1\Resources\Checkin\ReviewController;
use App\Http\Controllers\V1\Resources\Checkin\DutyController;
use App\Http\Controllers\V1\Resources\Checkin\EmployeeController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::group(
    [
        'prefix' => 'v1',
    ],
    function () {

        /*
          * 需要授权路由组
        */
        Route::group(['middleware' => 'checkToken'], function () {
            Route::group(
                [
                    'prefix' => 'users',
                    'as'     => 'users.'
                ],
                function () {
                    // 获取用户信息
                    Route::get('info', [UserController::class, 'info'])->name('info');
                    // 修改用户信息
                    Route::post('update', [UserController::class, 'update'])->name('update');
                }
            );
            // 安全管理
            Route::group(
                [
                    'prefix' => 'safety',
                    'as'     => 'safety.',
                ],
                function () {
                    // 风险源
                    Route::group(['prefix' => 'risk_sources', 'as' => 'risk_sources.'], function () {
                        // 查询风险列表
                        Route::post('list', [RiskSourceController::class, 'list'])->name('list');
                        // 添加风险源
                        Route::post('add', [RiskSourceController::class, 'add'])->name('add');
                        // 修改风险源
                        Route::post('update', [RiskSourceController::class, 'update'])->name('update');
                        // 删除风险源
                        Route::post('delete', [RiskSourceController::class, 'delete'])->name('delete');
                        // 查询风险源详情
                        Route::get('detail/{id}', [RiskSourceController::class, 'detail'])->name('detail');
                    });
                    // 安全隐患
                    Route::group(['prefix' => 'hazards', 'as' => 'hazards.'], function () {
                        // 查询安全隐患列表
                        Route::post('list', [SafetyHazardController::class, 'list'])->name('list');
                        // 添加安全隐患
                        Route::post('add', [SafetyHazardController::class, 'add'])->name('add');
                        // 修改安全隐患
                        Route::post('update', [SafetyHazardController::class, 'update'])->name('update');
                        // 删除安全隐患
                        Route::post('delete', [SafetyHazardController::class, 'delete'])->name('delete');
                        // 查询安全隐患详情
                        Route::get('detail/{id}', [SafetyHazardController::class, 'detail'])->name('detail');
                    });
                    // 生产事故
                    Route::group(['prefix' => 'accidents', 'as' => 'accidents.'], function () {
                        // 查询生产事故列表
                        Route::post('list', [ProductionAccidentController::class, 'list'])->name('list');
                        // 添加产事故
                        Route::post('add', [ProductionAccidentController::class, 'add'])->name('add');
                        // 修改产事故
                        Route::post('update', [ProductionAccidentController::class, 'update'])->name('update');
                        // 删除产事故
                        Route::post('delete', [ProductionAccidentController::class, 'delete'])->name('delete');
                        // 查询产事故详情
                        Route::get('detail/{id}', [ProductionAccidentController::class, 'detail'])->name('detail');
                    });
                    // 问题清单
                    Route::group(['prefix' => 'questions', 'as' => 'questions.'], function () {
                        // 查询问题列表
                        Route::post('list', [QuestionController::class, 'list'])->name('list');
                        // 添加问题
                        Route::post('add', [QuestionController::class, 'add'])->name('add');
                        // 修改问题
                        Route::post('update', [QuestionController::class, 'update'])->name('update');
                        // 删除问题
                        Route::post('delete', [QuestionController::class, 'delete'])->name('delete');
                        // 查询问题详情
                        Route::get('detail/{id}', [QuestionController::class, 'detail'])->name('detail');
                    });
                }
            );
            // 消息
            Route::group(
                [
                    'prefix' => 'messages',
                    'as'     => 'messages.'
                ],
                function () {
                    // 查询消息列表
                    Route::get('list', [MessageController::class, 'list'])->name('list');
                    // 删除消息
                    Route::post('delete', [MessageController::class, 'delete'])->name('delete');
                    // 查询消息详情
                    Route::get('detail/{id}', [MessageController::class, 'detail'])->name('detail');
                    // 改变消息状态
                    Route::post('change_status', [MessageController::class, 'changeStatus'])->name('change_status');
                    // 批量标记已读
                    Route::post('markAllRead', [MessageController::class, 'markAllRead'])->name('mark_all_read');
                    // 反馈保存
                    Route::post('feedback', [FeedbackController::class, 'add'])->name('feedback');
                }
            );

            // 签到管理
            Route::group(
                [
                    'prefix' => 'checkin',
                    'as'     => 'checkin.'
                ],
                function () {
                    // 提交签到
                    Route::post('submit', [CheckinController::class, 'submit'])->name('submit');
                    // 获取签到日历
                    Route::get('calendar', [CheckinController::class, 'calendar'])->name('calendar');
                    // 获取签到详情
                    Route::get('detail/{date}', [CheckinController::class, 'detail'])->name('detail');
                    // 获取签到记录列表
                    Route::get('records', [CheckinController::class, 'records'])->name('records');
                    // 获取打卡记录列表（新接口）
                    Route::get('list', [CheckinController::class, 'list'])->name('list');
                    // 检查指定日期签到状态
                    Route::get('check', [CheckinController::class, 'check'])->name('check');
                    // 获取今日签到状态
                    Route::get('today-status', [CheckinController::class, 'todayStatus'])->name('today_status');
                }
            );

            // 审核管理
            Route::group(
                [
                    'prefix' => 'review',
                    'as'     => 'review.'
                ],
                function () {
                    // 班长提交审核
                    Route::post('leader/submit', [ReviewController::class, 'leaderSubmit'])->name('leader.submit');
                    // 主管提交审核
                    Route::post('supervisor/submit', [ReviewController::class, 'supervisorSubmit'])->name('supervisor.submit');
                    // 检查班长审核状态
                    Route::get('leader/status', [ReviewController::class, 'leaderStatus'])->name('leader.status');
                    // 检查主管审核状态
                    Route::get('supervisor/status', [ReviewController::class, 'supervisorStatus'])->name('supervisor.status');
                    // 获取班长待审核数据
                    Route::get('leader/pending', [ReviewController::class, 'leaderPending'])->name('leader.pending');
                    // 获取主管待审核数据
                    Route::get('supervisor/pending', [ReviewController::class, 'supervisorPending'])->name('supervisor.pending');
                    // 获取主管视图：班组状态和组员打卡明细
                    Route::get('supervisor/teams-view', [ReviewController::class, 'supervisorTeamsView'])->name('supervisor.teams_view');
                    // 获取审核详情
                    Route::get('detail/{date}/{team}', [ReviewController::class, 'detail'])->name('detail');
                    // 获取审核记录列表
                    Route::get('records', [ReviewController::class, 'records'])->name('records');
                    // 获取审核统计
                    Route::get('statistics', [ReviewController::class, 'statistics'])->name('statistics');
                }
            );

            // 值班管理
            Route::group(
                [
                    'prefix' => 'duty',
                    'as'     => 'duty.'
                ],
                function () {
                    // 设置当班班长
                    Route::post('leader/set', [DutyController::class, 'setLeader'])->name('leader.set');
                    // 设置值班领导
                    Route::post('supervisor/set', [DutyController::class, 'setSupervisor'])->name('supervisor.set');
                    // 获取当班班长列表
                    Route::get('leader/list', [DutyController::class, 'leaderList'])->name('leader.list');
                    // 获取值班领导列表
                    Route::get('supervisor/list', [DutyController::class, 'supervisorList'])->name('supervisor.list');
                    // 获取值班人员详情
                    Route::get('supervisor/detail', [DutyController::class, 'supervisorDetail'])->name('supervisor.detail');
                    // 获取可选班长列表
                    Route::get('available-leaders', [DutyController::class, 'availableLeaders'])->name('available_leaders');
                    // 获取可选主管列表
                    Route::get('available-supervisors', [DutyController::class, 'availableSupervisors'])->name('available_supervisors');
                    // 获取今日值班信息
                    Route::get('today', [DutyController::class, 'today'])->name('today');
                    // 自动生成下一工作日当班班长
                    Route::post('auto-generate', [DutyController::class, 'autoGenerate'])->name('auto_generate');
                }
            );

            // 员工管理
            Route::group(
                [
                    'prefix' => 'employees',
                    'as'     => 'employees.'
                ],
                function () {
                    // 获取员工列表
                    Route::get('list', [EmployeeController::class, 'list'])->name('list');
                    // 获取班组列表
                    Route::get('teams', [EmployeeController::class, 'teams'])->name('teams');
                    // 获取职位列表
                    Route::get('positions', [EmployeeController::class, 'positions'])->name('positions');
                    // 获取当前用户信息和权限
                    Route::get('profile', [EmployeeController::class, 'profile'])->name('profile');
                    // 按班组获取员工列表
                    Route::get('by-team', [EmployeeController::class, 'byTeam'])->name('by_team');
                    // 按职位获取员工列表
                    Route::get('by-position', [EmployeeController::class, 'byPosition'])->name('by_position');
                    // 搜索员工
                    Route::get('search', [EmployeeController::class, 'search'])->name('search');
                }
            );

            // 认证相关（需要token）
            Route::group(
                [
                    'prefix' => 'auth',
                    'as'     => 'auth.'
                ],
                function () {
                    // 验证公司部门信息
                    Route::post('verify', [LoginController::class, 'verify'])->name('verify_with_token');
                }
            );

            // 用户相关（需要token）
            Route::group(
                [
                    'prefix' => 'user',
                    'as'     => 'user.',
                ],
                function () {
                    // 绑定员工
                    Route::post('bind', [UserController::class, 'bind'])->name('bind');
                    // 预览头像
                    Route::post('preview-avatar', [UserController::class, 'previewAvatar'])->name('preview_avatar');
                    // 获取头像服务状态
                    Route::get('avatar-service-status', [UserController::class, 'getAvatarServiceStatus'])->name('avatar_service_status');
                }
            );
        });

        // 无需认证
        Route::group(
            [
                'prefix' => 'auth',
                'as'     => 'auth.',
            ],
            function () {
                Route::post('wxlogin', [LoginController::class, 'login'])->name('login');
                Route::post('login/refresh', [LoginController::class, 'refresh'])->name('refresh');
                Route::post('login/logout', [LoginController::class, 'logout'])->name('logout');
                Route::get('user/info', [UserController::class, 'getUserByOpenid'])->name('user.info');
            }
        );

        // 设置
        Route::group(
            [
                'prefix' => 'options',
                'as'     => 'options.',
            ],
            function () {
                Route::get('teams', [ConfigController::class, 'teams'])->name('teams');
                Route::get('positions', [ConfigController::class, 'positions'])->name('positions');
                Route::get('place', [ConfigController::class, 'place'])->name('place');
                Route::get('about', [ConfigController::class, 'about'])->name('about');
            }
        );

        Route::group(
            [
                'prefix' => 'components',
                'as'     => 'components.',
            ],
            function () {
                // 上传文件
                Route::post('uploads', [UploadController::class, 'upload'])->name('uploads');
            }
        );
    }
);
